/**
 * 解析url参数
 * @param {String} url 地址
 */
export function getUrlParams(url) {
  const regx = new RegExp(/.*?\?\w+=\w+(&\w+=\w+)*$/);
  if (!regx.test(url)) return {};
  const subfix = url.split('?')[1];
  const params = subfix.split('&');
  const result = {};
  params.forEach(item => {
    const items = item.split('=');
    result[items[0]] = items[1];
  });
  return result;
}

/**
 * @description 对象不为空
 */
export function isNotEmpty(obj) {
  return obj !== undefined && obj !== null;
}

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path);
}

/**
 * @param {Sting} url
 * @param {Sting} title
 * @param {Number} w
 * @param {Number} h
 */
export function openWindow(url, title, w, h) {
  // Fixes dual-screen position Most browsers Firefox
  const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screen.left;
  const dualScreenTop = window.screenTop !== undefined ? window.screenTop : window.screen.top;

  let width; let
    height;
  if (window.innerWidth) {
    width = window.innerWidth;
  } else {
    width = document.documentElement.clientWidth
      ? document.documentElement.clientWidth
      : window.screen.width;
  }
  if (window.innerHeight) {
    height = window.innerHeight;
  } else {
    height = document.documentElement.clientHeight
      ? document.documentElement.clientHeight
      : window.screen.height;
  }

  const left = ((width / 2) - (w / 2)) + dualScreenLeft;
  const top = ((height / 2) - (h / 2)) + dualScreenTop;
  const newWindow = window.open(
    url,
    title,
    `toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=${w}, height=${h}, top=${top}, left=${left}`,
  );

  // Puts focus on the newWindow
  if (window.focus) {
    newWindow.focus();
  }
}
